<template>
  <FullPage>
    <VGrid
      ref="vgrid"
      :formConfig="formConfig"
      :tableConfig="tableConfig"
      :proxy="proxy"
      @handleFormBtnClick="handleFormBtnClick"
      @handleTableBtnClick="handleTableBtnClick"
    ></VGrid>
    <PriviledgeFormDialog ref="departmentFormDialog" @submit="getTableData"></PriviledgeFormDialog>
  </FullPage>
</template>

<script>
import FullPage from '@/components/FullPage';
import VGrid from '@/components/VGrid';
import PriviledgeFormDialog from './PriviledgeFormDialog';

export default {
  components: {
    VGrid,
    FullPage,
    PriviledgeFormDialog,
  },

  data() {
    return {
      formConfig: {
        props: {
          inline: true,
        },
        btns: [
          {
            key: 'addPrivilege',
            label: '新增权限',
            type: 'success',
            icon: 'el-icon-plus',
          },
        ],
        fieldList: [],
      },
      tableConfig: {
        props: {
          border: true,
          stripe: true,
          rowKey: 'id',
        },
        columnList: [
          {
            prop: 'name',
            label: '权限名称',
            align: 'left',
          },
          {
            prop: 'code',
            label: '权限 code',
          },
          {
            prop: 'sort',
            label: '权限序号',
          },
          {
            prop: 'type',
            label: '类型',
            formatter({ cellValue }) {
              if (cellValue === 'function') {
                return '功能';
              }
              return '菜单';
            },
          },
          {
            prop: 'urlType',
            label: '请求类型',
          },
          {
            prop: 'url',
            label: 'URL',
          },
          {
            label: '操作',
            type: 'operation',
            btns: [
              {
                key: 'addSubPrivilege',
                label: '新增子权限',
              },
              {
                key: 'edit',
                label: '编辑',
              },
              {
                key: 'delete',
                label: '删除',
              },
            ],
          },
        ],
      },
      proxy: {
        list: {
          api: this.$api.account.privilege.getPrivilegeList,
          resultKey: '',
        },
        delete: {
          api: this.$api.account.privilege.deletePrivilege,
        },
      },
    };
  },

  methods: {
    handleFormBtnClick(key, params) {
      if (key === 'addPrivilege') {
        this.$refs.departmentFormDialog.show('add');
      }
    },
    handleTableBtnClick(key, row) {
      if (key === 'edit') {
        this.$refs.departmentFormDialog.show('edit', row);
      }

      if (key === 'addSubPrivilege') {
        this.$refs.departmentFormDialog.show('add', row);
      }
    },

    getTableData() {
      this.$refs.vgrid.getTableData();
    },
  },
};
</script>

<style lang="scss" scoped></style>
